
var toltal = 59;
var listType = "buy";
var curPage = 1;
var type = 0;
var temp = 0;
var ph = 0;
var search = null;
var bh = 0;
 
$(document).ready(function() {
	type=  parent.document.getElementById("ty").value;
	
	search =  parent.document.getElementById("sea").value;
	if(search=="oo"){
		search = null;
	}	
	if(type==null){
		type=0;
	}	
	$("#mu a").each(function() {
		if ($(this).attr("type") == type) {
			$(this).addClass("now1");
		} else {
			$(this).removeClass("now1");
		}
	});
	getTotal(curPage);
	getPage(curPage);
});

function sh(name){
	search = name;
	bh=1;
	getTotal(curPage);
	getPage(curPage);
}

function getType(slt) {
	type = slt;
	$("#mu a").each(function() {
		if ($(this).attr("type") == type) {
			$(this).addClass("now1");
		} else {
			$(this).removeClass("now1");
		}
	});

	$(".tag a").each(function() {
		$(this).removeClass("now2");
	});
	getTotal(curPage);

	getPage(curPage);
}


function getTemp(slt) {
	temp = slt;
	$(".tag a").each(function() {
		if ($(this).attr("temp") == temp) {
			$(this).addClass("now2");
		} else {
			$(this).removeClass("now2");
		}
	});
	getPage(curPage);
}

function getTotal(curPage) {
	$.ajax({
		type : "post",
		async : false,
		url : "getGood.action",
		dataType : "json",
		data : {
			"listTypes" : listType,
			"types" : type,
			"temps" : temp,
			"curPages" : curPage,
			"toltal" : 0,
			"search":search,
			"top" : bh
		},
		success : function(result) {
			toltal = result;
		}
	});
}

function getLength() {
	var height = $('#here').outerHeight(true);
	var Y = $('#here').position().top;
	ph = height + Y - 50;
}

function getData(curPage) {
	$.ajax({
		type : "post",
		async : false,
		url : "getGood.action",
		dataType : "json",
		data : {
			"listTypes" : listType,
			"types" : type,
			"temps" : temp,
			"curPages" : curPage,
			"toltal" : 1,
			"search":search,
			"top" : bh
		},
		success :getGoods
	}); 
}

var content = "";
function getGoods(data) {
	content = data;
	var outputb = "";
	if(data==null){
		outputb ='<h3>没有你要找的商品</h3>';
	}else{
		for ( var i = 0; i < data.length; i++) {
			var name = data[i].uid;
			outputb += '<div class="box">';
			outputb += '<table border="0"><tr><td style="width: 40%;text-align: left">';
			outputb += '<a onclick="javascript:showper(\''+name+'\',event)"><img src="../uImg/'+data[i].uid+'.jpg"/></a></td>';
			outputb += '<td style="padding-top: 20px;vertical-align: top;font-weight:bold">';
			outputb += '<a onclick="javascript:showper(\''+name+'\',event)">' + data[i].userName + '</a><br> <br>';
			outputb += '<span style="font-size: 12px;color:gray">'
					+ data[i].createDate + '</span></td></tr>';
			outputb += '<tr><td colspan="2" style="width: 120px;text-align:left;padding-left:2px">';
			outputb += '<a href="detail.action?goodId=' + data[i].goodId + '">'
					+ data[i].goodName + '</a><br /> <br />数量 :' + data[i].count
					+ '<br />';
			outputb += '<span style="font-weight:bold;color:crimson">估价 :'
					+ data[i].price + 'RMB</span></td>';
			outputb += '</tr></table>';
			outputb += '</div>';
		}
	}
	
	document.getElementById("here").innerHTML = outputb;
	getLength();
}

function getPage(curPage) {
	getData(curPage);
	supage(curPage, toltal);
}

function supage(curPage, total) {
	var output = '<div class="pagination" style="top:' + ph + 'px">';
	var pageSize = 16;
	if (parseInt(total) == 0 || parseInt(total) == 'NaN')
		return;
	var totalPage = Math.ceil(total / pageSize);
	var curPage = parseInt(curPage) > 0 ? parseInt(curPage) : 1;

	if (totalPage > 5) {
		if ((curPage - 2) > 0 && curPage <= totalPage - 2) {
			var start = curPage - 2;
			var end = curPage + 2;
		} else if (curPage >= (totalPage - 2)) {
			var start = totalPage - 2;
			var end = totalPage;
	   }
	} else {
		var start = 1;
		var end = totalPage;
	}

	//首页控制   
	if (curPage > 1) {
		output += '<a href="javascript:getPage(1);"   title="第一页" class="page-first">首页</a>';
	} else {
		output += '<span class="page-disabled">首页</span> ';
	}
	//上一页菜单控制   
	if (curPage > 1) {
		output += '<a href="javascript:getPage(' + (curPage - 1)
				+ ');"  title="上一页" class="page-previous">上一页</a>';
	} else {
		output += '<span class="page-disabled">上一页</span>';
	}

	//页码展示   
	for (i = start; i <= end; i++) {
		if (i == curPage) {
			output += '<a href="javascript:;" class="page-cur">' + curPage
					+ '</a>';
		} else {
			output += '<a href="javascript:getPage(' + i + ');">' + i + '</a>';
		}
	}
	//下一页菜单控制   
	if (totalPage > 1 && curPage < totalPage) {
		output += '<a title="下一页" href="javascript:getPage(' + (curPage + 1)
				+ ');"  class="page-next">下一页</a>';
	} else {
		output += '<span class="page-disabled">下一页</span>';
	}
	//最后页控制   
	if (curPage < totalPage) {
		output += '<a title="最后页"   href="javascript:getPage(' + totalPage
				+ ');" class="page-end">尾页</a>';
	} else {
		output += '<span class="page-disabled">尾页</span>';
	}

	output += '</div>';

	document.getElementById("pageNav").innerHTML = output;
};


var x=0;
var y=0;
var sy =0;
function GetPostion(e) {
	   x = getX(e);
	   y = getY(e);
	   sy = getSY(e);
	}
	function getX(e) {
	  e = e || window.event;   
	return e.pageX || e.clientX + document.body.scroolLeft;
	}

	function getY(e) {
	  e = e|| window.event;
	 return e.clientY;
	} 
	
	function getSY(e) {
		  e = e|| window.event;
		 return e.screenY;
		} 



function showper(uid,e) {
	 GetPostion(e);
	for ( var i = 0; i < content.length; i++) {
		if (uid == content[i].uid) {
			$("#img").attr("src","../uImg/"+uid+".jpg");
			$("#name").text(content[i].userName);
			$("#pv").text("信誉度："+content[i].credit);
			if(content[i].sign==null){
				$("#sign").text("这位用户什么也没留下");
			}else{
				$("#sign").text(content[i].sign);
			}		
			$("#add").attr("onclick","addFriend('"+uid+"')");
		}
	}
	    var left;
	    var top;
	    
	    (x>650) ? left=(x-390):left=x;
	    (sy>410) ? top=(y-300):top=y;
	    
	    $(".per").css({position:"absolute",left:left,top:top}).delay(400).fadeIn("slow");
}

function closeper() {
	$(".per").fadeOut("slow");
}

function addFriend(uid){
	$(".per").fadeOut("slow");
	$.ajax({
		type : "post",
		async : false,
		url : "addF.action",
		dataType : "text",
		data : {"fid" : uid},
		success : function(msg){
			alert(msg);
		}
	});
}
